package com.spring.demo3.eg1;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * cglib代理(接口)
 * @author vhans
 */
public class CostTimeInterfaceProxy implements MethodInterceptor {
    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        // 这里针对接口，接口方法无内容，不能在这里调用
        System.out.println("方法：" + method.getName());
        return null;
    }

    public static Object createProxy(Class<?>[] classes) {
        Enhancer enhancer = new Enhancer();
        enhancer.setInterfaces(classes);
        enhancer.setCallback(new CostTimeInterfaceProxy());
        return enhancer.create();
    }
}
